package com.yq.core.facade.impl;

import com.yq.api.service.UserServiceFacade;
import com.yq.api.comm.ResultHandle;
import com.yq.api.dto.UserTO;
import com.yq.core.dto.UserDO;
import com.yq.core.util.UserDOUtil;
import com.yq.core.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;

/**
 * Created by yq on 16-5-26.
 */
@Stateless(name = "userServiceFacade")
public class UserServiceFacadeImpl implements UserServiceFacade {
    private static final Log log = LogFactory.getLog(UserServiceFacadeImpl.class);
    @EJB(beanName = "userService")
    private UserService userService;
    public ResultHandle<UserTO> addUser(UserTO userTO) {
        ResultHandle<UserTO> resultHandle = new ResultHandle<UserTO>();

        try{
            UserDO userDO= UserDOUtil.toUserDO(userTO);
            userService.addUser(userDO);
            //回写用户的信息
            UserDOUtil.writeBack(userDO, userTO);
            resultHandle.setSuccess(Boolean.TRUE);
            resultHandle.setResultContent(userTO);
        }catch (Exception e){
            log.error("Error occurs while add user:", e);
            resultHandle.setMessage(e.getMessage());
            resultHandle.setSuccess(Boolean.FALSE);
        }

        return resultHandle;
    }

    public ResultHandle<UserTO> findUser(Long userId) {
        ResultHandle<UserTO> resultHandle = new ResultHandle<UserTO>();
        try {
            UserDO userDO = userService.findUser(userId);
            UserTO userTO = UserDOUtil.toUserTO(userDO);
            resultHandle.setSuccess(Boolean.TRUE);
            resultHandle.setResultContent(userTO);
        } catch (Exception e) {
            log.error("Error occurs while find user:" + userId, e);
            resultHandle.setMessage(e.getMessage());
            resultHandle.setSuccess(Boolean.FALSE);
        }
        return resultHandle;
    }

    public ResultHandle<List<UserTO>> getUserList(UserTO userTO) {
        ResultHandle<List<UserTO>> resultHandle = new ResultHandle<List<UserTO>>();
        try {
            UserDO userDO = UserDOUtil.toUserDO(userTO);
            List<UserDO> userList = userService.getUserList(userDO);
            List<UserTO> userTOList = UserDOUtil.toUserTOList(userList);
            resultHandle.setSuccess(Boolean.TRUE);
            resultHandle.setResultContent(userTOList);
        } catch (Exception e) {
            log.error("Error occurs while find user list:", e);
            resultHandle.setMessage(e.getMessage());
            resultHandle.setSuccess(Boolean.FALSE);
        }
        return null;
    }

    public ResultHandle<UserTO> deleteUser(UserTO userTO) {
        ResultHandle<UserTO> resultHandle = new ResultHandle<UserTO>();
        if(userTO == null){
            resultHandle.setSuccess(Boolean.FALSE);
            resultHandle.setMessage("Can't delete null object, will do nothing");
            return resultHandle;
        }

        try {
            UserDO userDO = UserDOUtil.toUserDO(userTO);
            userService.deleteUser(userDO);
            resultHandle.setSuccess(Boolean.TRUE);
            resultHandle.setResultContent(userTO);
        } catch (Exception e) {
            log.error("Error occurs while delete user:" + userTO.getUserId(), e);
            resultHandle.setMessage(e.getMessage());
            resultHandle.setSuccess(Boolean.FALSE);
        }
        return resultHandle;
    }

    public ResultHandle<UserTO> updateUser(UserTO userTO) {
        ResultHandle<UserTO> resultHandle = new ResultHandle<UserTO>();
        if(userTO == null){
            resultHandle.setSuccess(Boolean.FALSE);
            resultHandle.setMessage("Can't update null object, will do nothing");
            return resultHandle;
        }

        try {
            UserDO userDO = UserDOUtil.toUserDO(userTO);
            userService.updateUser(userDO);
            resultHandle.setSuccess(Boolean.TRUE);
            resultHandle.setResultContent(userTO);
        } catch (Exception e) {
            log.error("Error occurs while update user:" + userTO.getUserId(), e);
            resultHandle.setMessage(e.getMessage());
            resultHandle.setSuccess(Boolean.FALSE);
        }
        return resultHandle;
    }
}
